import React from "react";
import { Link, Routes, Route, Navigate } from "react-router-dom";

const Home = () => {
  return <div>这是home页面</div>;
};
const About = () => {
  return <div>这是about页面</div>;
};
const Mine = () => {
  return <div>这是mine页面</div>;
};
const Login = () => {
  return <div>这是login页面</div>;
};

const Auth = (props) => {
  return localStorage.getItem("token") ? (
    props.children
  ) : (
    <Navigate to="/login"></Navigate>
  );
};

const App = () => {
  return (
    <>
      <h2>路由鉴权</h2>

      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/mine">我的页</Link>
        </li>
      </ul>

      <Routes>
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        <Route
          path="/mine"
          element={
            <Auth>
              <Mine />
            </Auth>
          }
        ></Route>
        <Route path="/login" element={<Login />}></Route>
        <Route path="*" element={<div>404 notfound...</div>}></Route>
      </Routes>
    </>
  );
};

export default App;
